package com.zhuangfei.hos_timetableview.ability;

import com.zhuangfei.hos_timetableview.ResourceTable;
import com.zhuangfei.hos_timetableview.custom.CustomOperater;
import com.zhuangfei.hos_timetableview.model.MySubject;
import com.zhuangfei.hos_timetableview.model.SubjectRepertory;
import com.zhuangfei.hos_timetableview.utils.ToastUtil;
import com.zhuangfei.timetable.TimetableView;
import com.zhuangfei.timetable.listener.ISchedule;
import com.zhuangfei.timetable.listener.OnDateBuildAapter;
import com.zhuangfei.timetable.listener.OnSpaceItemClickAdapter;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.StackLayout;

import java.util.List;

/**
 * 基础功能演示：
 * 1.周次选择栏
 * 2.透明背景
 * 3.点击监听
 * 4.颜色分配
 * 5.日期高亮
 * 6.日期计算
 * <p>
 * <p>
 * 该界面的代码注释会比较详细，建议从此处开始看起
 */
public class CustomWidthAbility extends Ability {

    private static final String TAG = "CustomWidthActivity";

    // 控件
    TimetableView mTimetableView;
    CustomOperater operater;
    List<MySubject> mySubjects;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_custom_width);
        mySubjects = SubjectRepertory.loadDefaultSubjects2();
        mySubjects.addAll(SubjectRepertory.loadDefaultSubjects());
        initTimetableView();
    }

    /**
     * 初始化课程控件
     */
    private void initTimetableView() {
        // 获取控件
        mTimetableView = (TimetableView) findComponentById(ResourceTable.Id_id_timetableView);
        operater = new CustomOperater();
        operater.setWidthWeights(new float[]{1, 3, 1, 1, 1, 1, 1});
        mTimetableView.source(mySubjects)
                .curWeek(1)
                .maxSlideItem(10)
                .monthWidthVp(30)
                .operater(operater)
                // 旗标布局点击监听
                .callback(new ISchedule.OnFlaglayoutClickListener() {
                    @Override
                    public void onFlaglayoutClick(int day, int start) {
                        mTimetableView.hideFlaglayout();
                        ToastUtil.showToast(CustomWidthAbility.this, "点击了旗标:周" + (day + 1) + ",第" + start + "节");
                    }
                })
                .callback(new OnDateBuildAapter() {
                    @Override
                    public Component[] getDateViews(LayoutScatter mInflate, float monthWidth, float perWidth, int height) {
                        Component[] views = new Component[8];
                        views[0] = onBuildMonthLayout(mInflate, (int) monthWidth, height);
                        float[] weights = operater.getWeights();
                        float sum = 0;
                        for (int i = 0; i < weights.length; i++) {
                            sum += weights[i];
                        }
                        for (int i = 1; i < 8; i++) {
                            float rato = weights[i - 1] / sum;
                            views[i] = onBuildDayLayout(mInflate, i, (int) (7 * perWidth * rato), height);
                        }
                        return views;
                    }
                })
                .callback(new OnSpaceItemClickAdapter() {
                    @Override
                    public void onSpaceItemClick(int day, int start) {
                        // day:从0开始，start：从1开始
                        if (flagLayout == null) {
                            return;
                        }
                        float[] weights = operater.getWeights();
                        float sum = 0;
                        for (int i = 0; i < weights.length; i++) {
                            sum += weights[i];
                        }
                        int newItemWidth = (int) (super.itemWidth * 7 * weights[day] / sum);
                        float newMarLeft = 0;
                        for (int i = 0; i < day; i++) {
                            newMarLeft += (super.itemWidth * 7 * weights[i] / sum);
                        }
                        StackLayout.LayoutConfig lp = new StackLayout.LayoutConfig(newItemWidth - marLeft * 2, itemHeight);
                        lp.setMargins(monthWidth + (int) newMarLeft + marLeft, (start - 1) * (itemHeight + marTop) + marTop, 0, 0);
                        flagLayout.setLayoutConfig(lp);
                    }
                })
                .showView();
    }

    /**
     * 更新一下，防止因程序在后台时间过长（超过一天）而导致的日期或高亮不准确问题。
     *
     * @param intent 意图
     */
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);
        mTimetableView.onDateBuildListener().onHighLight();
    }

}
